import os
import tables

let x = "xxxx"
echo x.splitPath()
let y = "/xxx"
echo y.splitPath()
echo "/target/xxx".splitPath()
echo "target/xxx".splitPath()
echo "/target/xxx".splitPath().head.splitPath()
echo "target/xxx".splitPath().head.splitPath()


var map = initTable[string, bool]()

proc badDir(dirName: string): bool = 
  if map.hasKey(dirName):
    return map[dirName]
  const skipDirectories = ["target", "build", "node_modules", "dist", ".git"]
  for skipDir in skipDirectories:
    if dirName == skipDir or dirName == "/" & skipDir or dirName == "\\" & skipDir:
      map[dirName] = true 
      return true
  map[dirName] = false 
  return false 

proc shouldSkipDirectory(directoryName: string): bool =
  var tmpPath = directoryName 
  if badDir(tmpPath.splitPath.tail):
    return true 
  while tmpPath.splitPath.head != "" and tmpPath.splitPath.head != "/" and tmpPath.splitPath.head != "\\": 
    if badDir(tmpPath.splitPath.tail):
      return true 
    tmpPath = tmpPath.splitPath.head
    if badDir(tmpPath.splitPath.tail):
      return true  
    
  return false

echo shouldSkipDirectory("xxxx")
echo shouldSkipDirectory("xxx/xxx")
echo shouldSkipDirectory("/xxx/xxx")
echo shouldSkipDirectory("/target/xxx")
echo shouldSkipDirectory("/xxx/target")
echo shouldSkipDirectory("xxx/target")
echo shouldSkipDirectory("target/xxx")
echo shouldSkipDirectory("\\src")
echo shouldSkipDirectory("\\src\\target")